package com.spongebob.controller.gsp;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spongebob.pojo.*;
import com.spongebob.service.*;
import com.sun.org.apache.bcel.internal.generic.MONITORENTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * <p>
 * 客户信息 前端控制器
 * </p>
 *
 * @author tutu
 * @since 2021-03-16
 */
@Controller
@RequestMapping("/customerManage")
public class CustomerInfoController {

    @Autowired
    CustomerInfoService customerInfoService;

    @Autowired
    CustomerServiceService customerServiceService;

    @Autowired
    WorkQualityStatisticsService workQualityStatisticsService;

    @Autowired
    WorkOrderService workOrderService;

    @Autowired
    BlackListService blackListService;

    @Autowired
    GroupManageService groupManageService;

    /**
     * 删除客户信息
     * @param id  //前端页面需要删除客户的id
     * @param model // model
     * @return "redirect:/customerManage/customerTables"
     */
    @RequestMapping("/deleteCustomer")
    public String deleteCustomer(Integer id, Model model) {
        customerInfoService.removeById(id);
        model.addAttribute("list", customerInfoService.list());
        return "redirect:/customerManage/customerTables";
    }

    /**
     * 添加客户
     * @param customerInfo //前端新增客户信息表单拿到的客户信息
     * @return "redirect:/customerManage/customerTables"
     */
    @RequestMapping("/addCustomer")
    public String addCustomer(CustomerInfo customerInfo) {

        Date date = new Date();
        customerInfo.setCreatetime(date);
        customerInfo.setLastedlogintime(date);
        customerInfo.setUpdatetime(date);

        customerInfoService.save(customerInfo);

        return "redirect:/customerManage/customerTables";
    }

    /**
     * 修改客户信息
     * @param updateC //前端修改客户信息表单拿到的客户信息
     * @param session //将当前需要修改信息的客户id保存到session
     * @return "redirect:/customerManage/individualCustomerInfo/?id=" + currentId
     */
    @RequestMapping("/updateCustomer")
    public String updateCustomer(CustomerInfo updateC, Model model, HttpSession session) {

        Integer currentId = (Integer) session.getAttribute("CurrentId");
        CustomerInfo c = customerInfoService.getById(currentId);
        if (c == null) {
            c = new CustomerInfo();
        }
        model.addAttribute("c", c);

        updateC.setId(currentId);
        updateC.setUpdatetime(new Date());
        QueryWrapper<CustomerInfo> qw = new QueryWrapper();
        qw.eq("id", currentId);
        customerInfoService.update(updateC, qw);
        String s = "redirect:/customerManage/individualCustomerInfo/?id=" + currentId;

        return s;
    }

    /**
     * 客户信息界面
     * @param pageNum //当前页面
     * @param customername // 前端查询客户名时输入的关键字
     * @return "index"
     */
    @RequestMapping("/customerTables")
    public String tables(@RequestParam(value = "pageNum", defaultValue = "0") int pageNum,
                         Model model,String customername) {

        IPage<CustomerInfo> iPage;
        QueryWrapper qw = new QueryWrapper();
        qw.like("customername", customername);

        if (customername == null || customername == "") {
            iPage = new Page<CustomerInfo>(pageNum, 10);
            iPage = customerInfoService.page(iPage);
        } else {
            iPage = new Page<CustomerInfo>(pageNum, 30);
            iPage = customerInfoService.page(iPage, qw);
        }

        long iPageCurrent = iPage.getCurrent();
        long pages = iPage.getPages();

        List<CustomerInfo> list = iPage.getRecords();
        model.addAttribute("iPageCurrent", iPageCurrent);
        model.addAttribute("pages", pages);
        model.addAttribute("customerInfoList", list);
        model.addAttribute("status", 5);

        return "index";
    }


    /**
     * 创建工单
     * @param id //指定为当前客户创建工单
     * @param workOrder //从前端创建工单表单中拿到的数据
     * */
    @RequestMapping("/createWorkOrder")
    public String createWorkOrder(Integer id, WorkOrder workOrder, HttpSession session){

        session.setAttribute("ccid",id);
        CustomerService user = (CustomerService)session.getAttribute("user");

        QueryWrapper qw = new QueryWrapper();
        qw.eq("groupname",user.getServicegroup());
        GroupManage groupManage = groupManageService.getOne(qw);

        if (groupManage == null){
            session.setAttribute("gmid","0");
        }else{
            session.setAttribute("gmid",groupManage.getGroupid());
        }

        workOrder.setCustomerid(id);
        workOrder.setCustomerserviceid(user.getCustomerserviceid());
        workOrder.setCreatetime(new Date());
        workOrder.setUpdatetime(new Date());

        workOrderService.save(workOrder);

        return "redirect:/customerManage/customerTables";
    }


    /**
     * 客户个人详细信息
     * @param id //当前客户id
     * @param model //
     * @param session //
     * @return "index"
     * */
    @RequestMapping("/individualCustomerInfo")
    public String customerInfo(Integer id, Model model, HttpSession session ) {
        session.setAttribute("CurrentId", id);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String stringCreateTime = new String() ;
        String stringLastedTime = new String() ;
        String stringUpdateTime = new String();
        long serviceTime = 0 ;
        CustomerInfo c = customerInfoService.getById(id);
        if (c == null) {
            c = new CustomerInfo();
        }else{
            stringCreateTime = sdf.format(c.getCreatetime());
            stringLastedTime = sdf.format(c.getLastedlogintime());stringUpdateTime = sdf.format(c.getUpdatetime());
            serviceTime = c.getCreatetime().getTime() - c.getLastedlogintime().getTime();
        }

        /***
         * 历史会话
         * 来源----客户信息表、工作质量统计表
         * ${#dates.format(user.time, 'yyyy-MM-dd HH:mm')}
         * */

        model.addAttribute("c", c);
        model.addAttribute("stringCreateTime", stringCreateTime);
        model.addAttribute("stringLastedTime", stringLastedTime);
        model.addAttribute("stringUpdateTime", stringUpdateTime);
        model.addAttribute("serviceTime", serviceTime / 1000 / 60);
        //获得 当前客户对应客服ID
        CustomerService cs = customerServiceService.getById(c.getCustomerserviceid());
        if (cs == null) {
            cs = new CustomerService();
        }
        //获取 wqs 工作质量统计的 满意度
        WorkQualityStatistics wqs = workQualityStatisticsService.getById(cs.getCustomerserviceid());
        int goodReview;
        if (wqs == null) {
            goodReview = 0;
        } else {
            goodReview = wqs.getGoodreview();
        }
        model.addAttribute("goodReview", goodReview);//工作质量数据

        /**
         * 历史工单
         * 来源----工作订单表（workOrder)
         * */

        QueryWrapper qw = new QueryWrapper();
        qw.eq("customerid", c.getId());

        // 通过客户ID，查询相关订单集
        List<WorkOrder> WorkOrderList = workOrderService.list(qw);
        String[] updateTime = new String[WorkOrderList.size() + 1];
        int i = 0;
        for (WorkOrder o : WorkOrderList) {
            updateTime[++i] = sdf.format(o.getUpdatetime());
        }
        model.addAttribute("updateTime", updateTime);
        model.addAttribute("WorkOrderList", WorkOrderList);//历史工单
        model.addAttribute("cs", cs);//客服信息


        /***
         * 拉黑记录
         * */

        List<BlackList> blackLists = blackListService.list(qw);

        String[] blackTime = new String[blackLists.size() + 1];
        String[] realName = new String[blackLists.size() + 1];
        int j = 0;
        for (BlackList bl : blackLists) {
            j++;
            blackTime[j] = sdf.format(bl.getTime());
            CustomerService customerService = customerServiceService.getById(bl.getCustomerserviceid());
            if (customerService == null) {
                realName = null;
            } else {
                realName[j] = customerService.getRealname();
            }

        }

        model.addAttribute("blackLists", blackLists);
        model.addAttribute("realName", realName);
        model.addAttribute("blackTime", blackTime);
        model.addAttribute("status", 51);


        return "index";
    }
}

